package edu.hawaii.webspider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.net.ConnectException;
import org.junit.Test;

/**
 * Test case for web spider, which checks to see that basic functionality is in place
 * and that no errors are thrown.
 *
 * @author phuoc le
 * This test class writen by Jared Sunouchi
 */
public class TestWebSpiderExample {

  /** The url used for testing WebSpiderExample. */
  public static final String url = "http://hackystat.org";
  /** The task name for the totalLinks method. */
  public static final String totallinks = "-totallinks";
  /** The task name for the mostPopular method. */
  public static final String mostpopular = "-mostpopular";
  /** The task name to include logging. */
  public static final String logging = "-logging";
  
  /**
   * Test WebSpider method to get the number of links on a website page.
   *
   * @exception Exception If errors occur during method execution.
   */
  @Test
  public void testNormalOperation() throws Exception {
    WebSpiderExample spider = new WebSpiderExample(url, false);
    int numberOfLinks = spider.getNumLinks();
    assertEquals("Testing getNumLinks().", 1, numberOfLinks);
    String[] args1 = {totallinks, url, "5"};
    WebSpiderExample.main(args1);
    String[] args2 = {mostpopular, url, "5"};
    WebSpiderExample.main(args2);
    String[] args3 = {totallinks, url, "5", logging};
    WebSpiderExample.main(args3);
    String[] args4 = {mostpopular, url, "5", logging};
    WebSpiderExample.main(args4);
  }
  
  /** 
   * Test illegal totalLinks.
   *  
   * @exception Exception If errors occur during method execution.
   */
  @Test
  public void testIllegalTotalLinks() throws Exception {
    try {
      String[] args = {totallinks, url, "28", logging};
      WebSpiderExample.main(args);
    }
    catch (ConnectException error) {
      fail("Unreachable website exception was not caught.");
      System.out.println("The specified link is not reachable.");
    }
  }
  
  /** 
   * Test illegal mostPopular.
   *  
   * @exception Exception If errors occur during method execution.
   */
  @Test
  public void testIllegalMostPopular() throws Exception {
    try {
      String[] args = {mostpopular, url, "28", logging};
      WebSpiderExample.main(args);
    }
    catch (ConnectException error) {
      fail("Unreachable website exception was not caught.");
      System.out.println("The specified link is not reachable.");
    }
  }
  
  /** 
   * Test illegal argument input for main method.
   *  
   * @exception Exception If errors occur during method execution.
   */
  @Test
  public void testIllegalMain() throws Exception {
    try {
      String[] args1 = {url, mostpopular, "26"};
      WebSpiderExample.main(args1);
      String[] args2 = {totallinks, url, logging, "26"};
      WebSpiderExample.main(args2);
      String[] args3 = {mostpopular, url};
      WebSpiderExample.main(args3);
      String[] args4 = {"-incorrecttaskname", url, "26"};
      WebSpiderExample.main(args4);
    }
    catch (Exception error) {
      fail("Input pameter error was not caught.");
      System.out.println("Input parameter error.");
    }
  }
}
